#!/usr/bin/env python
# -*- coding: utf-8 -*-


import os

import wx


###########################################################################
# Class WxEditPrjConfigEvent
###########################################################################
class WxPathSelectionChangedEvent(wx.PyCommandEvent):

    def __init__(self, evt_type, id):
        wx.PyCommandEvent.__init__(self, evt_type, id)
        self.paths = []

    def set_paths(self, paths):
        self.paths = paths

    def get_paths(self):
        return self.paths

PATH_SELECTION_CHANGE_TYPE = wx.NewEventType()
EVT_PATH_SELECTION_CHANGE = wx.PyEventBinder(PATH_SELECTION_CHANGE_TYPE, 1)


###########################################################################
# Class PathPanel
###########################################################################
class PathPanel(wx.Panel):
    def __init__(self, *args, **kwargs):

        wx.Panel.__init__(self, *args, **kwargs)

        self.cur_path = ''
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Fit(self)
        control = wx.TreeCtrl
        self.path_tree_ctrl = tree_ctrl = \
            control(self, id=wx.ID_ANY, style=wx.TR_DEFAULT_STYLE | wx.TR_FULL_ROW_HIGHLIGHT | wx.TR_MULTIPLE)

        self.sizer.Add(self.path_tree_ctrl, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
        self.init_imgs()

        self.root_node = None

        # event connection
        tree_ctrl.Bind(wx.EVT_TREE_ITEM_EXPANDING,      self.on_path_expanding)
        tree_ctrl.Bind(wx.EVT_TREE_ITEM_COLLAPSING,     self.on_path_collapsing)
        tree_ctrl.Bind(wx.EVT_TREE_SEL_CHANGED,         self.on_tree_selection_changed)
        # bug: edit can not be vetoed
        #tree_ctrl.Bind(dv.EVT_DATAVIEW_ITEM_START_EDITING, self.on_tree_ctrl_editing)

    def set_path(self, path):

        tree_ctrl = self.path_tree_ctrl

        # load path
        if os.path.exists(path):
            if os.path.isdir(path):
                tree_ctrl.DeleteAllItems()
                self.root_node = root_node = tree_ctrl.AddRoot(path, data=wx.TreeItemData(path))
                self.add_item_recursion(path, root_node)

    def add_item_recursion(self, path, root_node):
        if os.path.isdir(path):
            lst = os.listdir(path)
            for i in lst:
                real_path = os.path.join(path, i)
                self.path_tree_ctrl.AppendItem(root_node, i, data=wx.TreeItemData(real_path))

    def on_path_expanding(self, event):
        tree_ctrl = self.path_tree_ctrl
        cur_item = event.GetItem()
        path = tree_ctrl.GetItemData(cur_item).GetData()
        tree_ctrl.DeleteChildren(cur_item)
        self.add_item_recursion(path, cur_item)
        child_node, _ = tree_ctrl.GetFirstChild(cur_item)
        while child_node.IsOk():
            child_path = tree_ctrl.GetItemData(child_node).GetData()
            self.add_item_recursion(child_path, child_node)
            child_node = tree_ctrl.GetNextSibling(child_node)

    def on_path_collapsing(self, event):
        tree_ctrl = self.path_tree_ctrl
        cur_item = event.GetItem()
        path = tree_ctrl.GetItemData(cur_item).GetData()
        tree_ctrl.DeleteChildren(cur_item)
        self.add_item_recursion(path, cur_item)

    def on_tree_selection_changed(self, evt):
        tree_ctrl = self.path_tree_ctrl
        selections = tree_ctrl.GetSelections()
        paths = [tree_ctrl.GetItemData(item).GetData() for item in selections]

        path_changed_evt = WxPathSelectionChangedEvent(PATH_SELECTION_CHANGE_TYPE, self.GetId())
        path_changed_evt.set_paths(paths)
        self.GetEventHandler().ProcessEvent(path_changed_evt)

    def init_imgs(self):
        self.imgs = {}
        isz = (16,16)
        il = wx.ImageList(*isz)
        fldridx = il.AddIcon(wx.ArtProvider.GetIcon(wx.ART_FOLDER,      wx.ART_OTHER, isz))
        fldropenidx = il.AddIcon(wx.ArtProvider.GetIcon(wx.ART_FOLDER_OPEN, wx.ART_OTHER, isz))
        fileidx = il.AddIcon(wx.ArtProvider.GetIcon(wx.ART_NORMAL_FILE, wx.ART_OTHER, isz))
        self.path_tree_ctrl.SetImageList(il)

        self.imgs['fldridx'] = fldridx
        self.imgs['fldropenidx'] = fldropenidx
        self.imgs['fileidx'] = fileidx


